package com.winterchen.manager;

import com.microsoft.playwright.*;
import com.winterchen.model.Ywpt;
import com.winterchen.service.YwptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class BrowserManager {

    @Autowired
    private YwptService ywptService;

    public int openChrome() {

        try {
            Playwright playwright = Playwright.create();
            BrowserType chromium = playwright.chromium();

            Browser browser = chromium.launch(new BrowserType.LaunchOptions()
                    .setHeadless(false)
                    .setArgs(Arrays.asList("--start-maximized"))
                    .setChannel("chrome"));
            BrowserContext context = browser.newContext(new Browser.NewContextOptions().setViewportSize(null));
            Page page = context.newPage();
            page.navigate("https://www.baidu.com/");
            TimeUnit.SECONDS.sleep(3);
            browser.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 1;
    }

    public int updateEjk(Integer id) {
        ywptService.updateEjk(id);
        return 0;
    }

    public Ywpt selectYwptDetail(String shouLiId) {
        return ywptService.selectByShouLiId(shouLiId);
    }
}
